package com.alibaba.cirtus.ots.application.adapter.ability.facade.serviceV2.otw;

import com.alibaba.cirtus.ots.application.adapter.ability.facade.serviceV2.otw.common.OtwConstant;
import com.alibaba.cirtus.ots.application.adapter.ability.facade.serviceV2.otw.dto.ShippingOrderCreateDetailReq;
import com.alibaba.cirtus.ots.application.adapter.ability.facade.serviceV2.otw.dto.ShippingOrderCreateReq;
import com.alibaba.cirtus.ots.application.adapter.ability.model.Result;
import com.alibaba.cirtus.ots.application.adapter.ability.utils.HttpTokenUtil;
import com.alibaba.cirtus.ots.application.adapter.ability.utils.HttpUtils;
import com.alibaba.citrus.ots.common.annotation.ProcessInvoker;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.epoch.app.otsapplicationadaptersdk.contants.OtsApplicationAdapterSdkConstants;
import com.epoch.app.otsapplicationadaptersdk.mnotwif012.service.MnOtwIf012Service;
import com.epoch.app.otsapplicationadaptersdk.model.dto.MnOtwCommonResponse;
import com.epoch.app.otsapplicationadaptersdk.model.dto.MnOtwIf012Request;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@Primary
public class MnOtwIf012ServiceImpl implements MnOtwIf012Service {
    private final Log log = Log.getLogger(MnOtwIf012ServiceImpl.class);

    @Autowired
    private HttpUtils httpUtils;

    @Override
    @ProcessInvoker(remark = "MnOtwIf012Service", errorCode = "OTS-02-007-00-16-999", printLog = true)
    public MnOtwCommonResponse shippingOrderCreateSync(MnOtwIf012Request mnOtwIf012Request) {
        log.info("MnOtwIf012Service shippingOrderCreateSync param:{}", JSON.toJSONString(mnOtwIf012Request));
        Map<String, String> header = HttpTokenUtil.generateHttpHeader();
        String urlPrefix = OtsApplicationAdapterSdkConstants.urlPrefix();
        String url = urlPrefix + "api/mn_bc_link/createShippingOrder";
        MnOtwCommonResponse mnOtwCommonResponse = new MnOtwCommonResponse();
        try {
            ShippingOrderCreateReq shippingOrderCreateReq = new ShippingOrderCreateReq();
            List<ShippingOrderCreateDetailReq> detailReqList = mnOtwIf012Request.getDetailList().stream().map(detail -> {
                ShippingOrderCreateDetailReq shippingOrderCreateDetailReq = new ShippingOrderCreateDetailReq();
                BeanUtils.copyProperties(detail, shippingOrderCreateDetailReq);
                return shippingOrderCreateDetailReq;
            }).collect(Collectors.toList());
            BeanUtils.copyProperties(mnOtwIf012Request, shippingOrderCreateReq);
            shippingOrderCreateReq.setDetailList(detailReqList);
            log.info("shippingOrderCreateSync call bc link interface,url:{}, header:{}, param:{}", url, header, JSON.toJSONString(shippingOrderCreateReq));
            ResponseEntity<Result> post = httpUtils.post(url, header, shippingOrderCreateReq, Result.class);
            Result result = post.getBody();
            log.info("MnOtwIf010Service shippingOrderCreateSync mnOtwCommonResponse result:{}", JSON.toJSONString(result));
            if (result.isSuccess() &&  null != result.getResult()) {
                mnOtwCommonResponse.setSuccess(OtwConstant.SUCCESS_TRUE);
                mnOtwCommonResponse.setResult(result.getResult());
            } else {
                mnOtwCommonResponse.setSuccess(OtwConstant.SUCCESS_FALSE);
                mnOtwCommonResponse.setErrorCode(result.getErrorCode());
                mnOtwCommonResponse.setMessage(result.getMessage());
            }
        } catch (Exception e) {
            log.error("call bc-link shippingOrderCreateSync error", e);
            mnOtwCommonResponse.setSuccess(OtwConstant.SUCCESS_FALSE);
            mnOtwCommonResponse.setErrorCode(OtwConstant.ORDER_001_01_01_001);
            mnOtwCommonResponse.setMessage(OtwConstant.COMMON_MESSAGE);
        }
        return mnOtwCommonResponse;
    }
}
